package api

import (
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"video_client/handler/request"
	pb "video_client/proto"
)

func HomeDisplay(c *gin.Context) {
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewDemoClient(conn)
	display, _ := client.HomeDisplay(c, &pb.HomeDisplayReq{
		Uid: uint64(c.GetUint("userId")),
	})
	c.JSON(200, display)
}

func CreateWork(c *gin.Context) {
	var req request.CreateWorkReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewDemoClient(conn)
	work, _ := client.CreateWork(c, &pb.CreateWorkReq{
		Uid:      uint64(c.GetUint("userId")),
		Title:    req.Title,
		Info:     req.Info,
		WorkType: req.WorkType,
		Url:      req.Url,
		Status:   req.Status,
	})
	c.JSON(200, work)
}
